﻿using Orchard;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Drivers;
using Orchard.Localization;
using Orchard.UI.Notify;
using Whatsnexx.Models;

namespace Whatsnexx.Drivers
{
    public class WhatsnexxSettingsPartDriver : ContentPartDriver<WhatsnexxSettingsPart> {

        private const string TemplateName = "Parts/WhatsnexxSettings";
        private IOrchardServices _services;

        public WhatsnexxSettingsPartDriver(IOrchardServices services) 
        {
            _services = services;
            T = NullLocalizer.Instance;
        }

        public Localizer T { get; set; }

        protected override string Prefix { get { return "WhatsnexxSettings"; } }


        protected override DriverResult Editor(WhatsnexxSettingsPart part, dynamic shapeHelper)
        {
            return ContentShape("Parts_WhatsnexxSettings_Edit",
                                () => shapeHelper.EditorTemplate(TemplateName: TemplateName, Model: part, Prefix: Prefix)).OnGroup("whatsnexx");
        }

   


        protected override DriverResult Editor(WhatsnexxSettingsPart part, IUpdateModel updater, dynamic shapeHelper)
        {

            return ContentShape("Parts_WhatsnexxSettings_Edit", () =>
            {
                var previousPassword = part.Password;
                updater.TryUpdateModel(part, Prefix, null, null);

                // restore password if the input is empty, meaning it has not been reseted
                if (string.IsNullOrEmpty(part.Password))
                {
                    part.Password = previousPassword;
                }
                return shapeHelper.EditorTemplate(TemplateName: TemplateName, Model: part, Prefix: Prefix);
            })
               .OnGroup("whatsnexx");
        }
    }
}
